class Solution {
public:
    int mySqrt(int x) {
        int left = 0;
        int right = x;
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            // 1 2 3 4 5 6 7 8
            if ((long long)mid * mid == x)
            {
                return mid;
            }
            else if ((long long)mid * mid > x)
            {
                right = mid - 1;
            }
            else
            {
                left = mid + 1;

            }
        }
        return right;
    }
};